import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document } from 'mongoose'

@Schema({ timestamps: true })
export class TemplateRating extends Document {
    @Prop({ required: true })
    templateId: string

    @Prop({ required: true })
    userId: string

    @Prop({ required: true, min: 1, max: 5 })
    rating: number

    @Prop({ type: String })
    comment?: string

    @Prop({ type: Boolean, default: false })
    isEdited: boolean

    @Prop({ type: Date, default: Date.now })
    createdAt: Date

    @Prop({ type: Date, default: Date.now })
    updatedAt: Date
}

export const TemplateRatingSchema = SchemaFactory.createForClass(TemplateRating)

// 添加索引
TemplateRatingSchema.index({ templateId: 1, userId: 1 }, { unique: true })
TemplateRatingSchema.index({ templateId: 1, createdAt: -1 }) 